import { Controller, Get, Post, Body, Put, Param, Delete, Query, HttpCode, HttpStatus, UseGuards, Request } from '@nestjs/common';
import { TagsService } from './tags.service';
import { CreateTagDto } from './dto/create-tag.dto';
import { UpdateTagDto } from './dto/update-tag.dto';
import { TagQueryDto } from './dto/tag-query.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';

@Controller('tags')
@UseGuards(JwtAuthGuard)
export class TagsController {
  constructor(private readonly tagsService: TagsService) {}

  @Get()
  async findAll(@Query() query: TagQueryDto, @Request() req) {
    const data = await this.tagsService.findAll(query, req.user.id);
    return {
      code: 200,
      message: 'success',
      data,
      timestamp: Date.now(),
    };
  }

  @Get(':id')
  async findOne(@Param('id') id: string, @Request() req) {
    const data = await this.tagsService.findOne(+id, req.user.id);
    return {
      code: 200,
      message: 'success',
      data,
      timestamp: Date.now(),
    };
  }

  @Post()
  async create(@Body() createTagDto: CreateTagDto, @Request() req) {
    const data = await this.tagsService.create(createTagDto, req.user.id);
    return {
      code: 200,
      message: '创建成功',
      data,
      timestamp: Date.now(),
    };
  }

  @Put(':id')
  async update(@Param('id') id: string, @Body() updateTagDto: UpdateTagDto, @Request() req) {
    const data = await this.tagsService.update(+id, updateTagDto, req.user.id);
    return {
      code: 200,
      message: '更新成功',
      data,
      timestamp: Date.now(),
    };
  }

  @Delete(':id')
  @HttpCode(HttpStatus.OK)
  async remove(@Param('id') id: string, @Request() req) {
    await this.tagsService.remove(+id, req.user.id);
    return {
      code: 200,
      message: '删除成功',
      data: null,
      timestamp: Date.now(),
    };
  }
}